<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布人昵称" prop="userName" label-width="100">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入发布人昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发布人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品标题" prop="playTitle">
        <el-input
          v-model="queryParams.playTitle"
          placeholder="请输入作品标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布类型" prop="playType">
        <el-select v-model="queryParams.playType" placeholder="请选择发布类型" clearable size="small" style="width:180px"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in playTypeTag" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="绑定家乡" prop="hometownName">
        <el-input
          v-model="queryParams.hometownName"
          placeholder="请输入绑定家乡"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品状态" prop="playStatus">
        <el-select v-model="queryParams.playStatus" placeholder="请选择作品状态" clearable size="small" style="width:180px"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in playStatusTag" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期"
                        end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateAllStatus(1)"
          v-hasPermi="['store:play:edit']"
        >批量上架
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateAllStatus(0)"
          v-hasPermi="['store:play:edit']"
        >批量下架
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="playList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="作品ID" align="center" prop="playId" width="180"/>
      <el-table-column label="用户昵称" align="center" prop="userName"/>
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="作品标题" align="center" prop="playTitle" width="150"/>
      <el-table-column label="发布类型" align="center" prop="playType">
        <template v-slot="scope">
          <dic-staus-tag :options="playTypeTag" :staus="scope.row.playType"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="绑定家乡" align="center" prop="hometownAddress"/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="作品状态" align="center" prop="playStatus">
        <template v-slot="scope">
          <dic-staus-tag :options="playStatusTag" :staus="scope.row.playStatus"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论数" align="left" prop="playReviewNum" width="80">
        <template slot-scope="scope"><span class="loss-num">{{ scope.row.playReviewNum }}</span></template>
      </el-table-column>
      <el-table-column label="点赞数" align="left" prop="playUpvoteNum" width="80">
        <template slot-scope="scope"><span class="info-num">{{ scope.row.playUpvoteNum }}</span></template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="playRemark"/>
      <el-table-column label="操作" align="center" fixed="right" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['store:play:query']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateStatus(scope.row,1)"
            v-hasPermi="['store:play:edit']"
          >上架
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateStatus(scope.row,0)"
            v-hasPermi="['store:play:edit']"
          >下架
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:play:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改特色游玩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户昵称 : " prop="userName">
          <span>{{ form.userName }}</span>
        </el-form-item>
        <el-form-item label="作品标题 : " prop="videoTitle">
          <span>{{ form.playTitle }}</span>
        </el-form-item>
        <el-form-item label="作品正文 : " prop="videoContent">
          <span>{{ form.playContent }}</span>
        </el-form-item>
        <el-form-item label="作品图片 : " prop="playCoverUrl" v-if="form.playCoverUrl !== undefined && form.playCoverUrl !== null ">
          <image-preview v-for="imgUrl in (form.playCoverUrl.split(','))" :key="imgUrl" :src="imgUrl" :width="200" :height="200" />
        </el-form-item>
        <el-form-item label="地理位置 : " prop="videoContent">
          <span>{{ form.playFullAddress }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改特色游玩对话框 -->
    <el-dialog :title="titleStatus" :visible.sync="openStatus" width="500px" append-to-body>
      <el-form ref="form" :model="formStatus" :rules="rulesStatus" label-width="80px">
        <el-form-item label="下架原因" prop="playRemark">
          <el-input v-model="formStatus.playRemark" placeholder="请输入下架原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelStatus">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  editStatus,
  delPlay,
  getPlay,
  listPlay,
  updatePlay
} from "@/api/store/play";

export default {
  name: "Play",
  data() {
    return {
      playTypeTag: [
        {value: '1', label: "采摘", cssClass: "success"},
        {value: '2', label: "露营", cssClass: "danger"},
        {value: '3', label: "农家乐", cssClass: "info"},
        {value: '4', label: "小风景", cssClass: "warning"},
        {value: '5', label: "小娱乐", cssClass: "warning"}
      ],
      playStatusTag: [
        {value: 0, label: "下架", cssClass: "warning"},
        {value: 1, label: "上架", cssClass: "success"}
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 特色游玩表格数据
      playList: [],
      // 弹出层标题
      title: "",
      titleStatus: "",
      // 是否显示弹出层
      open: false,
      openStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        hometownId: undefined,
        playTitle: undefined,
        playContent: undefined,
        playType: undefined,
        playCoverUrl: undefined,
        playProvinceId: undefined,
        playCityId: undefined,
        playCountyId: undefined,
        playAddress: undefined,
        playRegionName: undefined,
      },
      // 表单参数
      form: {},
      // 表单参数
      formStatus: {},
      // 表单校验
      rules: {},
      // 表单校验
      rulesStatus: {
        playRemark: [
          {required: true, trigger: "blur", message: "请输入下架原因"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询特色游玩列表 */
    getList() {
      this.loading = true;
      listPlay(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.playList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelStatus() {
      this.openStatus = false;
      this.reset();
    },
    addDateRange(params, dateRange) {
      let search = params;
      dateRange = Array.isArray(dateRange) ? dateRange : [];
      search['startDate'] = dateRange[0];
      search['endDate'] = dateRange[1];
      return search;
    },
    // 表单重置
    reset() {
      this.form = {
        playId: undefined,
        userId: undefined,
        hometownId: undefined,
        playTitle: undefined,
        playContent: undefined,
        playType: undefined,
        playCoverUrl: undefined,
        playProvinceId: undefined,
        playCityId: undefined,
        playCountyId: undefined,
        playAddress: undefined,
        playRegionName: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
      this.formStatus = {
        playId: undefined,
        type: undefined,
        playStatus: undefined,
        status: undefined,
        playRemark: undefined
      };
      this.resetForm("formStatus");

    },
    /** 修改按钮操作 */
    handleView(row) {
      this.loading = true;
      this.reset();
      const playId = row.playId
      getPlay(playId).then(response => {
        console.log(response)
        this.form = response.data;
        this.loading = false;
        this.open = true;
        this.title = "查看作品详情";
        this.videoDialog = true
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.playId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加特色游玩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const playId = row.playId || this.ids
      getPlay(playId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改特色游玩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if(this.formStatus.type === 0){
            updatePlay(this.formStatus).then(response => {
              this.$modal.msgSuccess("下架成功");
              this.openStatus = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else if(this.formStatus.type === 1){
            const playIds = this.ids;
            let videoData = {
              ids: playIds,
              status: this.formStatus.status,
              remark: this.formStatus.playRemark
            };
            editStatus(videoData).then(response => {
              this.$modal.msgSuccess("下架成功");
              this.openStatus = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      });
    },
    handleUpdateStatus(row, playStatus) {
      this.reset();
      const playId = row.playId
      let content = '是否确定上架作品？';
      if (playStatus === 0) {
        this.reset();
        this.openStatus = true;
        this.titleStatus = "是否确定下架作品？";
        this.formStatus.type = 0;
        this.formStatus.playId = playId;
        this.formStatus.playStatus = playStatus
      } else {
        this.buttonLoading = true;
        this.$modal.confirm(content).then(() => {
          this.buttonLoading = true;
          this.form.playId = playId
          this.form.playStatus = playStatus
          return updatePlay(this.form)
        }).then(() => {
          this.buttonLoading = false;
          this.getList();
          if (playStatus === 0) {
            this.$modal.msgSuccess("下架成功");
          } else {
            this.$modal.msgSuccess("上架成功");
          }
        }).finally(() => {
          this.buttonLoading = false;
        });
      }
    },
    handleUpdateAllStatus(status) {
      this.reset();
      let content = '是否确定上架作品？';
      if (status === 0) {
        this.openStatus = true;
        this.titleStatus = "是否确定下架作品？";
        this.formStatus.type = 1;
        this.formStatus.status = status
      } else {
        const playIds = this.ids;
        this.$modal.confirm(content).then(() => {
          this.loading = true;
          let videoData = {
            ids: playIds,
            status: status
          };
          return editStatus(videoData);
        }).then(() => {
          this.loading = false;
          this.getList();
          if (status === 0) {
            this.$modal.msgSuccess("下架成功");
          } else {
            this.$modal.msgSuccess("上架成功");
          }
        }).finally(() => {
          this.loading = false;
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const playIds = row.playId || this.ids;
      this.$modal.confirm('是否确定删除作品？删除后作品信息无法恢复，包括内容、评论和点赞。').then(() => {
        this.loading = true;
        return delPlay(playIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/play/export', {
        ...this.queryParams
      }, `play_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.over-num {
  color: #F56C6C;
  font-weight: bold;
}

.loss-num {
  color: #67C23A;
  font-weight: bold;
}

.info-num {
  color: #606266;
  font-weight: bold;
}
</style>
